How to Setup Magento with Multiple Stores and Domains

How to Setup Magento with Multiple Stores and Domains

https://www.properhost.com/support/kb/30/How-To-Setup-Magento-With-Multiple-Stores-And-Domains
One of the useful features in Magento is the ability to create multiple stores/websites that share the same Magento installation. This allows multiple store fronts to share a common code base and backend, making administration a lot easier. Stores can share customer base, product catalog and settings based on how you choose to configure your sites.

This tutorial will go through the steps of setting up multiple stores in Magento, and how to configure a domain for each store.

Prerequisites
Magento must be installed
Access to add new vhosts (virtual hosts). Most control panels (like cPanel) supports this.
URL structure
Before you start adding new stores to Magento, you should have a clear understanding of how each store will be addressed, i.e. how the URL will look like. You basically have three approaches:

Domain (e.g. www.store1.com and www.store2.com)
Subdomain (e.g. store1.mystore.com and store2.mystore.com)
Folder (e.g. mystore.com/store1/ and mystore.com/store2/)
How you choose to structure your stores is a matter of personal preference, and the steps for setting this up will be more or less the same. Generally, subdomains are often used to create localized or area-specific versions of the store (e.g. es.mybestshop.com and en.mybestshop.com). Since most users will choose either the domain or subdomain method, this is the method I will describe in this tutorial.

Server configuration
Before we create the new Magento stores, we need to configure each domain/subdomain to resolve to the same Magento installation. That is, the document root of each virtual host must be set to the file path where Magento is installed. How this is done depends on the web server and control panel. For this example, we are assuming the server is running the Apache Web Server and the cPanel control panel. The method will be relatively similar for other control panels.

Assuming your main domain is already set up in cPanel, we just need to add the additional domains/subdomains. We will use the Parked Domains feature in cPanel to add the additional domains. Parked domains are similar to aliases, so they will automatically resolve to the same file directory. If you chose the subdomain method, just create the subdomains and point them to the directory where Magento is installed.

Parked Domains Parked Domain in cPanel
Add new Magento store
The next step is to create the actual stores in Magento. Lets say we want to add a new store mysecondstore.com.

Create root category
If you want your websites to share the same catalog and categories you can skip this step.

Log into your Magento Admin Panel
Click on Catalog > Manage Categories
magento-categories
Click on Add Root Category
magento-root-catalog
Enter a name for the new category and make sure Is Active is set to True
Click on the Display Settings tab and set Is Anchor to True. This will show products listed in sub categories, and also enable product drill-down functionality (filtering) for the category.
magento-category-anchor
Click Save Category
Store configuration
Before we start configuring our stores, lets pause for a minute and explain the concepts of Websites, Stores, and Store Views in Magento. Websites are the top-most entity in Magento. If you want completely separate sites that do not share cart, shipping methods, etc., you should create separate Websites. Each Website has at least one Store, and each Store has at least one Store View. Multiple Stores can share cart, user sessions, payment gateways, etc., but have their own catalog structure. Finally, a Store is a collection of Store Views. Store Views change the way pages are presented, normally used to offer a site in different layouts or languages. These concepts can be a bit confusing at first and I recommend this webinar which explains the Magento multi-store retailing in detail.

Go to System > Manage Stores
Click Create Website and enter the following information:
(Skip this step if you don’t want separate Websites)
Name – enter a name for the new website
Code – enter a unique identifier for this website
Make a note of this code as you will need it later!
magento-create-website

Click Create Store and enter the following:
Website – select the website you just created from the dropdown;
Name – enter a name for the store;
Root Category – select the category you created in the previous step.
magento-add-store

Click Create Store View and the information as follows
Store – select the store you created in the previous step
Name – enter a name for the store view
Code – enter a unique identifier for this store view
Make a note of this code as you will need it later!
This is where you will add additional store views if you plan on setting up a site for each language you support for example.
magento-add-storeview

Now, go to System > Configuration > General
Make sure Default Config is selected as the Current Configuration Scope
magento-config-scope
On the Web tab, set Auto-redirect to Base URL to No and click Save Config
magento-web-settings
Now, change the Configuration Scope dropdown to you newly created website
Under the Web section, we now need to change the Secure Base URL and Unsecure Base URL settings. Uncheck the Use Default [STORE VIEW], and replace the URL’s with your corresponding domain name. Remember to include the trailing /.
magento-site-urls
Click Save Configuration
This completes the configuration of the new store. Repeat these steps for each additional store you want to add.

Domain mapping
So far we have added the additional domains to the server and configured the new store in Magento. Now we just need to glue it together by telling Magento which store to load based on the domain name the user is on.

On the server, open the .htaccess file, located in the root directory of your Magento installation, in your favorite text editor. You can also use the File Manager in your control panel.
Add the following lines (replace with actual domain names):

SetEnvIf Host www.domain1.com MAGE_RUN_CODE=domain1_com
SetEnvIf Host www.domain1.com MAGE_RUN_TYPE=website
SetEnvIf Host ^domain1.com MAGE_RUN_CODE=domain1_com
SetEnvIf Host ^domain1.com MAGE_RUN_TYPE=website

SetEnvIf Host www.domain2.com MAGE_RUN_CODE=domain2_com
SetEnvIf Host www.domain2.com MAGE_RUN_TYPE=website
SetEnvIf Host ^domain2.com MAGE_RUN_CODE=domain2_com
SetEnvIf Host ^domain2.com MAGE_RUN_TYPE=website


Note: the SetEnvIf directive is not supported by all web servers (e.g. LiteSpeed). In that case, the store code can be set in this way:

RewriteCond %{HTTP_HOST} www.domain1.com [NC]
RewriteRule .* – [E=MAGE_RUN_CODE:domain1_com]
RewriteCond %{HTTP_HOST} www.domain1.com [NC]
RewriteRule .* – [E=MAGE_RUN_TYPE:website]

RewriteCond %{HTTP_HOST} www.domain2.com [NC]
RewriteRule .* – [E=MAGE_RUN_CODE:domain2_com]
RewriteCond %{HTTP_HOST} www.domain2.com [NC]
RewriteRule .* – [E=MAGE_RUN_TYPE:website]

MAGE_RUN_CODE – this is the unique code chosen when you created the Magento Website / Store View
MAGE_RUN_TYPE – depending on whether you want to load a specific Website or Store View; set it to website or store, respectively.
Add an entry for each additional domain you have set up.

Save the file
Now navigate to you new site and verify that the correct Magento store is loaded. If not, make sure that the the MAGE_RUN_CODE match the one created earlier and that the domain resolves to the correct folder/path.

Note about Magento versions prior to 1.4
The ability to set store code using environmental variables were introduced in Magento 1.4. If you are running an older version of Magento, or your webserver does not support Apache environmental variables, you must set the store code using a different method.

Open index.php in any text editor
Look for the following line near the bottom of the file:
Mage::run($mageRunCode, $mageRunType);
Add the following lines right above this line:
switch($_SERVER[‘HTTP_HOST’]) {
case ‘domain1.com’:
case ‘www.domain1.com’:
$mageRunCode = ‘domain1_com’;
$mageRunType = ‘website’;
break;
case ‘domain2.com’:
case ‘www.domain2.com’:
$mageRunCode = ‘domain2_com’;
$mageRunType = ‘website’;
break;
}
Replace the domain, code and type according to your particular setup. Add additional case statements if you have more stores.

Save the file
You should now be able to see each respective store by browsing the URL.

Leave a Comment